/* -*- C++ -*- */

//=============================================================================
/*
* @author Feloweria <380374483@qq.com>
* Copyright (C) Feloweria
*
* $ID 2014-06-14 06:58:06 PM
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
* @version 1.0
*/
//=============================================================================
#ifndef FLOWRIA_USER_ACCEPT_INCLUDE
#define FLOWRIA_USER_ACCEPT_INCLUDE

#include "Network/Acceptor.h"
#include "UserSocket.h"

using namespace Felowria;

#define MAX_USER 10240

class TestServer;

class UserAcceptor :public Acceptor
{
public:
    UserAcceptor(const char *addr,int32 port);
    ~UserAcceptor();
    
public:
	void OnAccept(Socket *socket);
	 
	void SetServer(TestServer *server) {this->server = server;}

protected:
	Socket* Alloc()
	{
		for (int32 loop_time = 0, max_loop_time = static_cast<int32>(end - begin); loop_time < max_loop_time; ++loop_time, ++socket)
		{
			if (socket >= end)
				socket = begin;

			if (!socket->Connected())
				return socket;

			if (loop_time >= max_loop_time)
				return NULL;
			
		}

		return NULL;
	}

private:
    TestServer *server;

private:
	UserSocket *socket_list;

private:
	UserSocket *begin;
	UserSocket *end;
	UserSocket *socket;

};

#endif //FLOWRIA_USER_ACCEPT_INCLUDE
